package com.ptmall.app.ui.activity;

import android.os.Bundle;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.bean.CollectionBean;
import com.ptmall.app.bean.PageData;
import com.ptmall.app.bean.WalletRechargeBean;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.adapter.CollectionAdapter;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;
import com.ptmall.app.view.TitleBarView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CollectionActivity extends BaseMvpActivity {


    TitleBarView titleBarView;
    PullableListView mListview;
    PullToRefreshLayout mRefreshView;
    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    List<CollectionBean> mDataLists = new ArrayList<>();
    int page = 1;
    CollectionAdapter mCollectionAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_list);
    }

    @Override
    public void initViews() {
        titleBarView = (TitleBarView) findViewById(R.id.titlebar);
        mListview = (PullableListView) findViewById(R.id.listview);
        mRefreshView = (PullToRefreshLayout) findViewById(R.id.refresh_view);

        titleBarView.setText("商品收藏");
        titleBarView.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        mRefreshView.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page ++;
                getData();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mRefreshView.autoRefresh();
    }

    @Override
    public void initEvents() {
        mCollectionAdapter = new CollectionAdapter(getContext(),null);
        mListview.setAdapter(mCollectionAdapter);
    }

    private void getData(){
        Map<String,String> map = new HashMap<>();
        map.put("page",page+"");
        apiDataRepository.getCollectionList(map, new ApiNetResponse<PageData<CollectionBean>>() {
            @Override
            public void onSuccess(PageData<CollectionBean> datas) {
                try {
                    if (null != mRefreshView) {
                        mRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                        mRefreshView.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                    }
                    if (page == 1) {
                        mDataLists.clear();
                        if (null != datas && datas.getData().size() > 0) {
                            mDataLists.addAll(datas.getData());
                        } else {
                        }
                    } else {
                        if (null != datas && datas.getData().size() > 0) {
                            mDataLists.addAll(datas.getData());
                        } else {
                            showMsg(R.string.has_no_more);
                        }
                    }
                    mCollectionAdapter.pushData(mDataLists);
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        });
    }
}
